//
//  MenuViewController.swift
//  YOKO
//
//  Created by vivi on 16/5/20.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

class MenuViewController: UIViewController {
    @IBOutlet weak var loginOrLogoutButton: UIButton!
    @IBOutlet weak var contactCEOButton: UIButton!
    @IBOutlet weak var contactEngineerButton: UIButton!
    @IBOutlet weak var contactDesignerButton: UIButton!
    @IBOutlet weak var phoneLabel: UILabel!
    
    var contactWebViewController: UIViewController!
    
    var hasLogin: Bool {
        return (NSUserDefaults.getAccessToken() != nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //在用户点击的时候还没有Notification，从这里初始化
        if hasLogin { loginHandler() }
        else { logoutHandler() }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MenuViewController.loginHandler), name: YOKONotification.LOGIN_NOTIFICATION, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MenuViewController.logoutHandler), name: YOKONotification.LOGOUT_NOTIFICATION, object: nil)
    }
    
    //MARK: - Handler
    func loginHandler() {
        loginOrLogoutButton.setTitle("登出", forState: .Normal)
        
        contactEngineerButton.hidden = false
        contactDesignerButton.hidden = false
        contactCEOButton.hidden = false
        phoneLabel.hidden = false
        
        if let phoneNumberString = NSUserDefaults.standardUserDefaults().stringForKey("phone_number") {
            phoneLabel.text = phoneNumberString.substringToIndex(phoneNumberString.startIndex.advancedBy(3)) + "****" + phoneNumberString.substringFromIndex(phoneNumberString.startIndex.advancedBy(7))
        }
    }
    
    func logoutHandler() {
        loginOrLogoutButton.setTitle("登录获得云账号", forState: .Normal)
        
        contactEngineerButton.hidden = true
        contactDesignerButton.hidden = true
        contactCEOButton.hidden = true
        phoneLabel.hidden = true
    }
    
    @IBAction func loginButtonHandler() {
        
        if hasLogin {
            ApiLogin().logout()
            (self.parentViewController as? SlideViewController)?.slideIn()
        }
        else {
            let loginViewController = LoginViewController(nibName: "LoginView", bundle: nil)
            loginViewController.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
            presentViewController(loginViewController, animated: true) {
                complete in
                (self.parentViewController as? SlideViewController)?.slideIn()
            }
        }
    }
    
    @IBAction func contactButtonHandler(sender: UIButton) {
        guard hasLogin == true else { return }
        
        var feedbackType: Int = 0
        switch sender {
        case contactCEOButton:
            feedbackType = 1
        case contactDesignerButton:
            feedbackType = 3
        case contactEngineerButton:
            feedbackType = 2
        default:
            return
        }
        
        if contactWebViewController == nil {
            contactWebViewController = UIViewController(nibName: nil, bundle: nil)
            contactWebViewController.modalTransitionStyle = .CoverVertical
            contactWebViewController.view.backgroundColor = UIColor(red: 255.0 / 255.0, green: 251.0 / 255.0, blue: 250.0 / 255.0, alpha: 1.0)
            
            let contactWebView = UIWebView(frame: CGRectZero)
            contactWebViewController.view.addSubview(contactWebView)
            contactWebView.snp_makeConstraints {
                make in
                make.top.equalTo(contactWebViewController.view).offset(37.5)
                make.left.equalTo(contactWebViewController.view)
                make.right.equalTo(contactWebViewController.view)
                make.bottom.equalTo(contactWebViewController.view)
            }
            
            let cancelButton = UIButton(type: .Custom)
            cancelButton.setImage(UIImage(named: "ic_close_black"), forState: .Normal)
            cancelButton.addTarget(self, action: #selector(MenuViewController.contactCancelButtonHandler), forControlEvents: .TouchUpInside)
            cancelButton.contentMode = .ScaleAspectFit
            cancelButton.alpha = 0.4
            contactWebViewController.view.addSubview(cancelButton)
            cancelButton.snp_makeConstraints {
                make in
                make.top.equalTo(10.0)
                make.left.equalTo(10.0)
                make.size.equalTo(CGSizeMake(30.0, 30.0))
            }
        }
        
        let feedbackURL = "https://test1.yokolife.com/app/feedback/web?access_token=\(NSUserDefaults.getAccessToken()!)&feedbackType=\(feedbackType)"
        let feedbackRequest = NSURLRequest(URL: NSURL(string: feedbackURL)!)
        (contactWebViewController.view.subviews[0] as! UIWebView).loadRequest(feedbackRequest)
        
        presentViewController(contactWebViewController, animated: true, completion: nil)
    }
        
    func contactCancelButtonHandler() {
        contactWebViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}